using FastDevTemplate.Api.Attribute;
using FastDevTemplate.Api.Base;
using FastDevTemplate.Api.Model;
using FastDevTemplate.Api.Model.Base;
using FastDevTemplate.Service.Interface;
using FastDevTemplate.Service.Interface.Base;
using FastDevTemplate.Service.Interface.DTO;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace FastDevTemplate.Api.Controllers;

[EnableCors("AllowSpecificOrigin")]
public class PermissionController : BaseController
{
    private readonly IPermissionService _permissionService;

    public PermissionController(IPermissionService permissionService)
    {
        _permissionService = permissionService;
    }

    [HttpPost("setByClient")]
    [PermissionVerification(id:"uc/permission/add", describe:"添加权限", client:"uc")]
    public async Task<HttpResult> Add([FromBody] PermissionSetModel model)
    {
        var result = await _permissionService.SetListByServiceAsync(model.Client, model.Permissions);
        if (result <= 0)
            return BadResult();
        return OkResult();
    }

    [HttpPost("getListByRole")]
    [PermissionVerification(id:"uc/permission/get", describe:"获取权限", client:"uc")]
    public async Task<HttpResult<List<PermissionResultDto>>> GetListByRole([FromBody] KeyModel<string> model)
    {
        if (!ValidateModel(model).isValid)
            return BadResult<List<PermissionResultDto>>();
        var result = await _permissionService.GetSetAsync(
            item => item.RolePermissions.Any(
                r => r.RoleID == model.ID));
        return OkResult(result.ToList());
    }

    [HttpPost("getList")]
    [PermissionVerification(id:"uc/permission/get", describe:"获取权限", client:"uc")]
    public async Task<HttpResult<PageResultDto<PermissionResultDto>>> GetList([FromBody] PageRequestModel model)
    {
        var result = await _permissionService.GetPageAsync(
            model.PageIndex, model.PageSize);
        return OkResult(result);
    }
}